package com.villaday.idf.custom;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.model.SmsEntity;
import com.villaday.idf.model.allVipMembersEntity;
import com.villaday.idf.model.vipsGroupsEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.ToastUtil;


/**
 * 添加组员界面中修改用户分组提示对话框
 */
@SuppressLint("ValidFragment")
public class UpdateGroupRemarkDialog extends BaseDialog implements View.OnClickListener, OkHttpUtil.OnDataListener {
    TextView tvContent;
    TextView tvNoLongerRemind;
    Context context;

    private String groupName;
    private String rabit;

    private OnNoLongRemindListener mListener;

    private Gson gson = new GsonBuilder().create();
    private allVipMembersEntity.BodyBean bean;


    public static UpdateGroupRemarkDialog newInstance(Context context, String bean) {
        UpdateGroupRemarkDialog dialog = new UpdateGroupRemarkDialog(context);
        Bundle bundle = new Bundle();
        bundle.putString("bean", bean);
        dialog.setArguments(bundle);
        return dialog;
    }

    public UpdateGroupRemarkDialog(Context context) {
        this.context = context;
    }

    @SuppressLint("WrongViewCast")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);//必须放在setContextView之前调用
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_update_group_remark, null);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.setCancelable(true);
        //宽度撑满
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //进入退出动画
        window.setWindowAnimations(R.style.animTranslateTop);


        tvContent = view.findViewById(R.id.tv_dialog_content);
        tvNoLongerRemind = view.findViewById(R.id.tv_no_longer_remind);

        bean = gson.fromJson(getArguments().getString("bean"), allVipMembersEntity.BodyBean.class);
        groupName = bean.getGroup().getName();
        rabit = bean.getGroup().getBrokenPercent() + "";
        tvContent.setText("该用户是" + groupName + "成员，分佣比例" + rabit + "%");
        TextView btnCancel = (TextView) view.findViewById(R.id.tv_cancel_btn);
        TextView btnSaveGroup = (TextView) view.findViewById(R.id.tv_ok);

        btnCancel.setOnClickListener(this);
        btnSaveGroup.setOnClickListener(this);
        tvNoLongerRemind.setOnClickListener(this);


        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_ok: {  //保存分组
                if (this.mListener != null) {
                    mListener.setRemind(tvNoLongerRemind.isSelected(), bean);
                }
                dismiss();

            }
            break;

            case R.id.tv_cancel_btn: {  //取消分组
                dismiss();
            }
            break;

            case R.id.tv_no_longer_remind: {
                tvNoLongerRemind.setSelected(!tvNoLongerRemind.isSelected());
            }
            break;
        }

    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.sms.equals(url)) { //刷新Token
                LogeUtil.e("sms", json);
                SmsEntity se = gson.fromJson(json, SmsEntity.class);
                if (Constant.Strings.SUC.equals(se.getCode())) {
                    ToastUtil.initToast(context, se.getMsg());
                } else {
                    ToastUtil.initToast(context, se.getMsg());
                }
            }

        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    public interface OnNoLongRemindListener {
        public void setRemind(boolean remind, allVipMembersEntity.BodyBean bean);
    }

    public void setOnNoLongRemindListener(OnNoLongRemindListener listener) {
        this.mListener = listener;
    }


}
